<?php

namespace App\Http\Controllers\Api;

use App\model\Area;
use App\model\Coustomer;
use App\model\CustomerSign;
use App\model\CustomerZhongjianSign;
use App\model\Source;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Carbon;

class BaobiaoController extends Controller
{
    /**
     * 报表
     */
    public function index(Request $request){
        $year=$request->get('year','2019');
        $month=$request->get('month','3');
        $project_id=$request->get('project_id',1);
        $time=Carbon::create($year,$month,1,0,0,0);
;
        //渠道
        $customer=Coustomer::select('source_id',\DB::raw("count(customer_id) as value"))
            ->whereYear('created_at',$year)
            ->where('project_id',$project_id)
            ->whereMonth('created_at','=',$month)
            ->groupBy('source_id')
            ->pluck('value','source_id');

        $sum=$customer->sum();
       ;
        $source=Source::all()->map(function($value,$key)use ($customer,$sum){
              return [
                    'id'=>$value->source_id,
                    'num'=>$customer[$value->source_id] ?? 0,
                    'value'=> ($sum==0 ? 0 : number_format(($customer[$value->source_id] ?? 0)/$sum*100,2)),
                    'name'=>$value->name
                ];
        });

        //城市
        $i=1;

        $customerCity=Coustomer::select('city2',\DB::raw("count(customer_id) as value"))
            ->whereYear('created_at',$year)
            ->whereMonth('created_at','=',$month)
            ->where('project_id',$project_id)
            ->groupBy('city2')
            ->pluck('value','city2');
        $customerCitySum=$customerCity->sum();

        $customerCity=$customerCity->map(function($value,$key)use(&$i,$customerCitySum){
                return [
                      'id'=>$i++,
                      'value'=>$customerCitySum==0 ? 0 : number_format(($value ?? 0)/$customerCitySum*100,2),
                      'name'=>$key=='' ? '其他城市' :$key,
                    'num'=>$value
                ];
            })->all();

        $array=[];
        foreach ($customerCity as $v){
            $array[]=$v;
        }

        //面积
        $customerArea=Coustomer::select('area_id',\DB::raw("count(customer_id) as value"))
            ->whereYear('created_at',$year)
            ->whereMonth('created_at','=',$month)
            ->where('project_id',$project_id)
            ->groupBy('area_id')
            ->pluck('value','area_id');
        $areaSum=  $customerArea->sum();
        $area=Area::all()->map(function($value,$key)use ($customerArea,$areaSum){
            return [
                'id'=>$value->id,
                'value'=> ($customerArea[$value->id] ?? 0)*100,
                 'num'=>   $customerArea[$value->id] ?? 0,
                'name'=>$value->name
            ];
        });
        $areaSum=$area->sum('value');
        //类型
        $customerSign=CustomerZhongjianSign::join('customer','customer.customer_id','=','customer_zhongjian_sign.customer_id')
            ->select('sign_id',\DB::raw("count(id) as value"))
            ->whereYear('customer.created_at',$year)
            ->whereMonth('customer.created_at','=',$month)
            ->groupBy('sign_id')
            ->pluck('value','sign_id');
        $customerSignSum=$customerSign->sum();
        $sign=CustomerSign::all()->map(function($value,$key)use ($customerSign,$customerSignSum){
            return [
                'id'=>$value->id,
                'value'=>$customerSignSum==0 ? 0 : number_format(($customerSign[$value->id] ?? 0)/$customerSignSum*100,2),
                'name'=>$value->name,

            ];
        });
        $signSum=$sign->sum('value');

        //近6个月入客分析
        $customerMonth=Coustomer::select(\DB::raw("DATE_FORMAT(created_at,'%Y%m') as time"),\DB::raw("count(customer_id) as value"),'source_id')
            ->where('project_id',$project_id)
            ->groupBy('source_id','time')
           ->get()->toArray()
           ;
        $allSum= collect($customerMonth)->sum('value');


        $timeArray=[
            $time->year.sprintf("%02d", $time->month),
            $time->addMonths(-1)->year.sprintf("%02d", $time->month),
            $time->addMonths(-1)->year.sprintf("%02d", $time->month),
            $time->addMonths(-1)->year.sprintf("%02d", $time->month),
            $time->addMonths(-1)->year.sprintf("%02d", $time->month),
            $time->addMonths(-1)->year.sprintf("%02d", $time->month),
            ];

        $sourceD=Source::all()->map(function($value,$key)use ($customerMonth,$timeArray){


                return [
                    'name'=>$value->name,
                    'type'=>'line',
                    'stack'=>'总量',

                    'data'=>[
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[5])
                            ->first()['value'] ?? 0,
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[4])
                            ->first()['value'] ?? 0,
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[3])
                            ->first()['value'] ?? 0,
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[2])
                            ->first()['value'] ?? 0,
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[1])
                            ->first()['value'] ?? 0,
                        collect($customerMonth)
                            ->where('source_id','=',$value->source_id)
                            ->where('time','=',$timeArray[0])
                            ->first()['value'] ?? 0,

                    ]
                ];
        });






        return response()->json(
            [
                'code'=>1,
                'data'=>[
                  'qudao'=>[
                      'data'=>$source->toArray(),
                      'total'=>$sum
                      ],
                    'quyu'=>[
                        'data'=>$array,
                        'total'=>$customerCitySum
                    ],
                    'mianji'=>[
                        'data'=>$area->toArray(),
                        'total'=>$areaSum
                    ],
                    'leixing'=>[
                        'data'=>$sign->toArray(),
                        'total'=>$signSum
                    ],
                    'lastD'=>[
                        'total'=>$allSum,
                            'month'=>collect($timeArray)->map(function($value){
                                return  sprintf("%01d",substr($value,-2)).'月';
                            })->reverse()->values(),
                            'data'=>$sourceD
                    ]
                ],

            ]
        );
    }
}
